export default {
  props: {
    allDataList: {
      type: null,
      default: {}
    },
    activeIndex: {
      type: null,
      default: 0
    }
  },
  data() {
    return {
      searchVal: "",
      active: '',
      goodsData:[],
      saveGoodsData:[],
      selectLevelGoods: []
    };
  },
  mounted() {
    //滑动到底部触发事件
    // let unpackingGoods = document.getElementsByClassName("van-popup");
    // unpackingGoods[0].onscroll = function () {
    //   let scrollHeight = unpackingGoods[0].scrollHeight;
    //   let clientHeight = unpackingGoods[0].clientHeight;
    //   let scrollTop = Math.round(unpackingGoods[0].scrollTop);
    //   if(scrollHeight == (clientHeight + scrollTop)) {
    //   }
    // }
    this.getLevelData();
  },
  methods: {
    async getLevelData() {
      let data = await $http.get("plugin.new-blind-box.frontend.activity.activity-level", {id: this.allDataList.list[this.activeIndex].id});
      if (data.result) {
        for(let item of data.data.list) {
          this.selectLevelGoods.push({
            level_id: item.id,
            goods: [],
            level_name: item.level_name,
            id: item.id,
            pivot: item.pivot
          })
        }
        this.active = data.data.list[0].id;
        this.searchActivityGoods();
      } else {
        this.$toast(data.msg);
      }
    },
    async searchActivityGoods() {
      let data = await $http.post("plugin.new-blind-box.frontend.activity.search-activity-goods", {id: this.allDataList.list[this.activeIndex].id , level_id : this.active, search : {
        goods_keyword : this.searchVal
      }});
      if (data.result) {
        this.goodsData = data.data.goods;
        for(let item of this.goodsData) {
          item.level_id  = this.active;
          for(let citem of this.saveGoodsData){
            if(item.goods_id == citem.goods_id) {
              let index = this.goodsData.findIndex(vitem => vitem.goods_id == citem.goods_id);
              let cindex = this.saveGoodsData.findIndex(vitem => vitem.goods_id == citem.goods_id);
              if(index !== -1) {
                this.saveGoodsData.splice(cindex,1);
              }
              if(index !== -1) {
                this.$set(this.goodsData[index], 'checked', true);
              }
            }
          }
        }
        this.selectLevelGoods = this.filterData();
      } else {
        this.$toast(data.msg);
      }
    },
    cancal() {
      this.$emit('cancalEvent', false )
    },
    clickEvent(e) {
      this.$nextTick(() => {
        let unpackingGoods = document.getElementsByClassName("van-popup");
        unpackingGoods[0].scrollTop  = 0;
      })
      let newData = this.goodsData.filter(item => item.checked)
      this.saveGoodsData.push(...newData);

      this.searchActivityGoods();
    },
    // 跳转到订单详情
    goOrderPage() {
      let select_goods = this.filterData().map(item => {
        return {
          goods: item.goods,
          level_id: item.level_id
        }
      });
      this.$router.push(
        this.fun.getUrl("goodsorder", {}, {
          tag: "-2",
          goodsId: this.allDataList.list[this.activeIndex].goods_id,
          name:"newBlindBox",
          select_goods: JSON.stringify(select_goods.filter(item => item.goods.length)),
          activity_id: this.allDataList.list[this.activeIndex].id
        })
      );
    },
    filterData() {
      let checkedGoodsData = this.goodsData.filter(item => item.checked);
      let checkedData = [...checkedGoodsData,...this.saveGoodsData];
      let select_goods = JSON.parse(JSON.stringify(this.selectLevelGoods));
      for(let item of select_goods) {
        item.goods = []
      }
      for(let item of checkedData) {
        for(let citem of select_goods) {
          if(item.level_id == citem.level_id) {
            citem.goods.push({
              goods_id: item.goods_id,
              goods_option_id: item.goods_option_id
            })
          }
        }
        }
      return select_goods
    },
    // 复选框事件
    changeChecked(){
      this.selectLevelGoods = this.filterData();
    }
  },
  components: {  },
};
